var app = getApp();
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
Page({
  data: {
    typeId: 0,
    valueId: 0,
    content: '',
    commentPicture:[],
    goods:{},
  },
  onLoad: function (options) {
    var that = this;
    var goods = JSON.parse(options.goods);
    that.setData({
      goods: goods,
      valueId: goods.goods_id
    });
  },
  //取消
  onClose() {
    wx.navigateBack({
      delta: 1
    });
  },
  //发表
  onPost() {
    let that = this;
    if (!that.data.content) {
      util.showErrorToast('请填写评论')
      return false;
    }
    wx.showModal({
      title: '',
      content: '确定发表此评论？',
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({ title: '加载中...', success: function () {} });
          util.request(api.CommentPost, {
            typeId: that.data.typeId,
            valueId: that.data.valueId,
            content: that.data.content,
          }, 'POST').then(function (res) {
            if (res.errno === 0) {
              if (that.data.commentPicture.length > 0) {
                util.uploadimg({
                  commentid: res.data.commentid,
                  path: that.data.commentPicture,
                }).then(function (e) {
                  wx.hideLoading();
                  wx.showToast({
                    title: '评论成功',
                    complete: function () {
                      setTimeout(function(){
                        wx.navigateBack({
                          delta: 1
                        });
                      },2000);
                    }
                  });
                });
              } else {
                wx.hideLoading();
                wx.showToast({
                  title: '评论成功',
                  complete: function () {
                    setTimeout(function () {
                      wx.navigateBack({
                        delta: 1
                      });
                    }, 2000);
                  }
                });
              }
            }
          });
        }
      }
    });
  },
  //输入框输入事件
  bindInpuntValue(event){
    let value = event.detail.value;
    //判断是否超过140个字符
    if (value && value.length > 100) {
      return false;
    }
    this.setData({
      content: event.detail.value,
    })
  },
  //预览图片
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.commentPicture // 需要预览的图片http链接列表
    })
  },
  //选择图片
  chooseImage: function (e) {
    var that = this;
    if (that.data.commentPicture.length >= 4) {
      util.showErrorToast("照片数量已满");
      return;
    }
    wx.chooseImage({
      count: 4 - that.data.commentPicture.length,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          commentPicture: that.data.commentPicture.concat(res.tempFilePaths)
        });
      }
    })
  },
  //清除不需要图片
  clearImage: function (e) {
    var that = this;
    if (that.data.commentPicture.length <= 0){
      return;
    }
    var images = that.data.commentPicture;
    images.splice(e.target.dataset.imageIndex,1);
    that.setData({
      commentPicture: images,
    });
  },
})